//
//  DetailMessageViewController.m
//  Rong360
//
//  Created by tianliwei on 14-3-7.
//  Copyright (c) 2014年 tianliwei. All rights reserved.
//

#import "DetailMessageViewController.h"
#import "DetailMsgCell.h"
@interface DetailMessageViewController ()
@property (nonatomic, strong)NSArray *replies;
@property (nonatomic, strong)NSString *ques;
@property (nonatomic, strong)UILabel *quesLabel;
@property (nonatomic, strong)UILabel *ansLabel;
@end


static NSString *cellIdentifier = @"DetailMsgCell";


@implementation DetailMessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithQues:(NSString *)ques replies:(NSArray *)replies{
    self = [super init];
    if (self) {
        self.replies = replies;
        self.ques = ques;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"专家咨询";
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailMsgCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    CGSize sizeQues = [self.ques sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:CGSizeMake(260, 9999) lineBreakMode:NSLineBreakByCharWrapping];
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 26+sizeQues.height)];
    UILabel *titleLabel = [UILabel labelWithFrame:CGRectMake(20, 26, UI_SCREEN_WIDTH - 40, sizeQues.height) title:self.ques fontColor:APPCOLOR fontSize:16.0f];
    [titleLabel resizeToFit];
    [headerView addSubview:titleLabel];
    self.tableView.tableHeaderView = headerView;
//    self.tableView.backgroundView = [[UIImageView alloc]initWithImage:[[UIImage imageNamed:@"tf_ques"]stretchableImageWithLeftCapWidth:20 topCapHeight:20]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    MsgInfo *msgInfo = self.replies[indexPath.row];
    if (msgInfo) {
        CGSize sizeReply = [msgInfo.reply sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:CGSizeMake(260, 9999) lineBreakMode:NSLineBreakByCharWrapping];
        NSLog(@"====>%@\n",msgInfo.reply);
        NSLog(@"====>Index:%d height:%f",indexPath.row,sizeReply.height);
        return ceil(sizeReply.height+30);
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.replies.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.userInteractionEnabled = NO;
    MsgInfo *msgInfo = self.replies[indexPath.row];
    cell.mainLabel.text = msgInfo.reply;
    [cell.mainLabel resizeToFit];
    cell.subLabel.text = msgInfo.time;
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
